package com.ecplatform.cart.mapper;

import com.ecplatform.cart.pojo.po.CartItem;
import com.ecplatform.cart.pojo.vo.CartItemVO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface CartMapper {

    @Select("SELECT * FROM cart where product_id = #{productId} AND user_id = #{userId}")
    CartItem selectCartItemByProductId(@Param("productId") Long productId, @Param("userId") Long userId);

    @Insert("INSERT INTO cart (product_id, product_name, quantity, total_price, user_id) VALUES (#{productId}, #{name}, #{quantity}, #{totalPrice}, #{userId})")
    void insertNewCartItem(@Param("productId") Long productId, @Param("name") String name, @Param("totalPrice") BigDecimal totalPrice, @Param("quantity") Integer quantity, @Param("userId") Long userId);

    @Update("UPDATE cart SET quantity = #{quantity}, total_price = #{totalPrice} WHERE product_id = #{productId} AND user_id = #{userId}")
    void updateCartItemQuantity(@Param("productId") Long productId, @Param("quantity") Integer quantity, @Param("totalPrice") BigDecimal totalPrice, @Param("userId") Long userId);

    @Select("SELECT * FROM cart WHERE user_id = #{userId}")
    List<CartItemVO> getCartItems(@Param("userId") Long userId);
}
